package br.com.verde.exemploteca.functions
  /*
  * Placeholder syntax like: someFunction(someArg, _, otherArg)
  is equivalent with: (arg) => someFunction(someArg, arg, otherArg)
  if the type inferencer does not have enough context information
  to infer the type, say A, of arg, then you have to help it as follows:
  (arg: A) => someFunction(someArg, arg, otherArg)
  * Comment author: Luc Duponcheel
  */

object Constraints {
  
  def filter(a: String, constraint: (String) => Boolean) = if(constraint(a)) println(a + " PASSED !")
                                                           else println(a + " FAILED !")
  
  def filter(a: String, i: Int, constraint: (String, Int) => Boolean) = if(constraint(a, i)) println(a + " PASSED !")
                                                           else println(a + " FAILED !")
  
  def filter(a: String, b: String, constraint: (String, String) => Boolean) = if(constraint(a, b)) println(a + " PASSED !")
                                                           else println(a + " FAILED !")
  def emptyStringConstraint(s: String) = s.isEmpty
  
  def startWithAConstraint(s: String) = s.charAt(0) == 'A'
  
  def tautology(s: String) = true
  
  def hasAOnPositionConstraint(s: String, i: Int) = s.charAt(i).toLowerCase == 'a'
  
  def directContainsConstraint(s1: String, s2: String) = s1 contains s2 
  
  def reverseContainsConstraint(s1: String, s2: String) = s2 contains s1 
  
  def main(args : Array[String]) {
    filter("bcd", startWithAConstraint)
    filter("bcd", tautology)
    filter("Rocha", 4, hasAOnPositionConstraint _)
    filter("oc", "Rocha", directContainsConstraint _)
  }
}
